<?php
class location extends CI_Model {
	
	function getDistance($lat, $long, $dis) {
		$s = "(SELECT id_Location,
(GLength(
LineStringFromWKB(
  LineString(
    point, 
    GeomFromText('POINT($lat $long)')
  )
 )
)*111.12) AS distance
FROM location
HAVING distance < $dis
ORDER BY distance ASC)";
		$result = $this->db->query ( $s );
		// $resul = $this->db->get($s);
		return $result->result_array ();
	
		//return $resul->result;
	}
	function getLocation() {
		
		$this->db->select ( '*' );
		$this->db->from ( 'locations' );
		$b = $this->db->get ();
		if ($b->num_rows >= 1) {
			return $b->result ();
		} else
			return false;
	
	}
	function getLocation2($id_Location) {
		
		/*$a = "select * from locations where id_Location = '.$id_Location.'";
		$b = $this->db->query ( $a );*/
		$this->db->select ( 'x(point), y(point)' );
		$this->db->from ( 'location' );
		$this->db->where ( 'id_Location', $id_Location );
		$b = $this->db->get ();
		if ($b->num_rows >= 1) {
			return $b->result ();
		} else
			return false;
	
	}
	function addLocation($latitude, $longitude) {
		//$data = array ('latitude' => $latitude, 'longitude' => $longitude );
		//$data ="GeomFromText( 'POINT($latitude $longitude)' )";
		//$this->db->insert ( 'location', $data );
		$s = "INSERT INTO location (point) VALUES( GeomFromText( 'POINT($latitude $longitude)' ))";
		$this->db->query($s);
		return mysql_insert_id ();
	}

}
?>